package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.cache.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @RequestMapping("/query")
    public Object query(TravelQuery qo){
         Page<Travel> page =  travelService.query(qo);
         return JsonResult.success(page);
    }

    @RequestMapping("/detail")
    public Object detail(String id){
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }


    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @RequestMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(Travel travel, HttpServletRequest req){

        String token = req.getHeader("token");
        UserInfo info = userInfoRedisService.getUserByToken(token);
        travel.setUserId(info.getId());
        travelService.saveOrUpdate(travel);
        //返回JsonResult中的data，存储的是游记的id
        return JsonResult.success(travel.getId());
    }


    @RequestMapping("/testuserinfo")
    public JsonResult testUserInfo(@UserParam UserInfo userInfo){
        return JsonResult.success(userInfo);
    }

    @RequestMapping("/testuserinfo1")
    public JsonResult testUserInfo1(UserInfo userInfo){
        return JsonResult.success(userInfo);
    }


//
//    @RequestMapping("/saveOrUpdate1")
//    public JsonResult saveOrUpdate1(Travel travel, UserInfo userInfo){
//
//        travel.setUserId(userInfo.getId());
//        travelService.saveOrUpdate(travel);
//        //返回JsonResult中的data，存储的是游记的id
//        return JsonResult.success(travel.getId());
//    }





    @RequestMapping("/comments")
    public JsonResult comments(String travelId){
        return JsonResult.success(new ArrayList<>());
    }


}
